/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invite;
import it.polimi.registration.business.security.entity.SystemNotification;
import it.polimi.registration.business.security.entity.User;
import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Innotommy91
 */

@Stateless
public class SystemNotificationManager
{
    
    @PersistenceContext
    EntityManager em;
   
    SystemNotification sn;
    
    @EJB
    private WeatherService WS;
   
  public void save(SystemNotification sn)
  {  
    System.out.println("Salvataggio notifica meteo");
    System.out.println("Salvataggio notifica per l'utente: "+sn.getUsername().getName());
    em.persist(sn);
  } 
  
  public void WeatherNotification(Event evento)
    {
        System.out.println("meteo?");
        
        WS.WeatherCheck(evento.getWeatherCondition());
        
        if(WS.getgood())
        {
            System.out.println(" meteo buono");//good time do nothing  
        }
        else
        {System.out.println("meteo cattivo");
            TypedQuery<Invite> query = em.createQuery("SELECT e FROM Invite e Where e.event = :id", Invite.class);
                query.setParameter("id",evento);
            List <Invite> lista= new ArrayList();
            lista= query.getResultList();
            int i;
            for(i=0;i<lista.size();i++)
            {
              sn= new SystemNotification(lista.get(i).getUser(),evento);
              em.persist(sn);
            }
        }
    }
  
    public List<SystemNotification> findNotification(User user){
        TypedQuery<SystemNotification> query = em.createQuery("SELECT i FROM SystemNotification i Where i.username = :mail AND i.event.endTime > :now", SystemNotification.class);
            query.setParameter("mail", em.find(User.class, user.getEmail()));
            query.setParameter("now", Calendar.getInstance());
        return query.getResultList();
    }   
    
    public List<SystemNotification> getNotification(Event event, User user){
        TypedQuery<SystemNotification> query = em.createQuery("SELECT i FROM SystemNotification i Where i.username = :mail AND i.event = :e", SystemNotification.class);
            query.setParameter("mail", em.find(User.class, user.getEmail()));
            query.setParameter("e", em.find(Event.class, event.getId()));       
        return query.getResultList();
    }
        
    
    public void lettaNotifica(User value,Event id)
    {
        SystemNotification sn;
        TypedQuery<SystemNotification> query = em.createQuery("SELECT e FROM SystemNotification e Where e.event = :event AND e.username = :user AND e.unread=:letta", SystemNotification.class);
                query.setParameter("event",id);
                query.setParameter("user",value);
                query.setParameter("letta", TRUE);
                sn=query.getSingleResult();
                sn.setUnread(FALSE);
    }
        
}  
